// pages/default/default.js
import { Default } from 'defaultsModel.js';
var defaults = new Default();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //中间导航数据
    daohang: [],
    //底部展示数据
    goodsAllList: [],
    //根路劲
    baseurl: 'https://wukongweixiu.cn/',
    // //随机显示订单
    suijidingdan: 0,
    //当前数据页数
    pageNum: 2,
    //加载更多 如果为true 则已经没有更多数据
    hasMore: true,

    addr: '',

    tle: '',
    
    inviteKey:'',
    showModal:0,
    url:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var inviteKey = options.invite;
    var that = this;
    if (inviteKey == undefined) {
      inviteKey = 0;
    }
    getApp().globalData.inviteKey = inviteKey;
    if (getApp().globalData.inviteKey != 0){
      that._settuijianre()
    }
    this._daohang();
    this._getAllGood();
    this._gettile()
  },

_settuijianre:function(){
  wx.request({
    //服务器的接口，将login（）获得的code发送的服务器换取session_key
    url: 'https://wukongweixiu.cn/index/Shere/setTuijian',
    data: {
      pid: getApp().globalData.inviteKey,
      id: getApp().globalData.id
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    method: 'GET',
    success: function (res) {
    },
    fail: function (res) { },
    complete: function (res) { }
  });
},

  _daohang: function () {
    var data = defaults.getdaohanginfo(this.callBake);
  },
  callBake: function (res) {
    getApp().globalData.tile = res.data.tile.tile;
    this.setData({
      daohang: res.data.data,
    })
  },


  _getAllGood: function () {
    var data = defaults.getAllGood(this.goodscallBake);
  },
  goodscallBake: function (res) {
    this.setData({
      goodsAllList: res.data.data.data,
    })
  },

  _gettile: function () {
    var data = defaults.gettile(this.gettilecallBake);
  },
  gettilecallBake: function (res) {
    
    this.setData({
      tle: res.data.data.tile,
    })
  },
  close:function(){
    this.setData({
      showModal:false
    })
  },
  coupon:function(){
    wx.navigateTo({
      url: '../coupon/coupon'
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that =this;
    that.startSetInter();
    that._getuserLaction();
    wx.request({
      //服务器的接口，将login（）获得的code发送的服务器换取session_key
      url: 'https://wukongweixiu.cn/index/Defaults/couponimg',
      data: {
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        that.setData({
            url:res.data.data.url,
            showModal:res.data.data.is_show,
          })
        
      },
      fail: function (res) { },
      complete: function (res) { }
    });

  },
  //定时器
  startSetInter: function () {
    var that = this;
    //将计时器赋值给setInter
    that.data.setInter = setInterval(
      function () {
        var numVal = that.data.suijidingdan;
        if (numVal == 1) {
          that.setData({ suijidingdan: 0 });
        } else {
          that.setData({ suijidingdan: 1 });
        }
      }, 9000);
  },


  //客服电话
  tocall: function () {
    wx.makePhoneCall({
      phoneNumber: getApp().globalData.tile
    })
  },
  //获取地理位置
  _getuserLaction: function () {
    // wx.getLocation({
    //   type: 'wgs84',
    //   success(res) {

    //     const latitude = res.latitude
    //     const longitude = res.longitude
    //     const speed = res.speed
    //     const accuracy = res.accuracy
    //     this.setData({
    //       addr: latitude + longitude + speed + accuracy,
    //     });
    //   }
    // })
  },
  //跳转搜索
  searchNav: function () {
    wx.navigateTo({
      url: '../search/search'
    });
  },
  xuqiu:function(){
    wx.navigateTo({
      url: '../tosendorder/tosendorder'
    });
  },


  //下拉加载
  onPullDownRefresh() { },
  onReachBottom() {
    var that = this;
    var hasMore = that.data.hasMore;
    if (hasMore == true) {
      var page = that.data.pageNum; 
      var res = defaults.getMoregoodsAllList((res) => {
     
        var data = res.data.data.data;
        if (data.length < res.data.data.per_page) {
            this.setData({
              goodsAllList: that.data.goodsAllList.concat(data),
              pageNum: res.data.data.current_page+1,
              hasMore: false,
            });
        } else {
          this.setData({
            goodsAllList: that.data.goodsAllList.concat(data),
            pageNum: res.data.data.current_page+1,
            
          });
        }
      }, page)

    } else {
      wx.showToast({
        title: '没有更多数据了！',
      })
    }
  },
  daohangs: function (e) {
    getApp().globalData.classid = e.currentTarget.dataset.id;
    wx.switchTab({
      url: '../classifys/classifys'
    });
  },
  todetail: function (e) {
    wx.navigateTo({
      url: '../detail/detail?id=' + e.currentTarget.dataset.id
    });
  },
  
  onShow: function () {
    var that = this;
    wx.login({
      success: res => { //获取code
            //发起网络请求
            wx.request({
              //服务器的接口，将login（）获得的code发送的服务器换取session_key
              url: 'https://wukongweixiu.cn/index/Defaults/sendCode',
              data: {
                js_code: res.code,
                type:1
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              method: 'GET',
              success: function (res) {
               
                getApp().globalData.userid = res.data.data.unionid;
                getApp().globalData.id = res.data.data.userid;

               
                if (res.data.data.userid == ''){
                  wx.navigateTo({
                    url: '../level1/level1'
                  });
                }
              },
              fail: function (res) { },
              complete: function (res) { }
          });
        }
    });

  }
})